package com.imti.mall.sale.controller;

import com.imti.mall.common.Api.CommonPage;
import com.imti.mall.common.Api.CommonResult;
import com.imti.mall.model.sms.pojo.SmsHomeAdvertise;
import com.imti.mall.model.sms.pojo.SmsHomeNewProduct;
import com.imti.mall.model.sms.pojo.SmsHomeRecommendSubject;
import com.imti.mall.model.sms.vo.SmsHomeAdvertiseVo;
import com.imti.mall.model.sms.vo.SmsHomeNewProductVo;
import com.imti.mall.sale.dao.SmsHomeAdvertiseDao;
import com.imti.mall.sale.service.SmsHomeAdvertiseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/sale")
@Api(tags = "首页轮播广告表")
public class SmsHomeAdvertiseController {
    @Autowired
    SmsHomeAdvertiseService smsHomeAdvertiseService;

    @PostMapping("/findSmsHomeAdvertise")
    @ApiOperation("查询首页轮播广告表")
    public CommonResult findSmsHomeAdvertise(SmsHomeAdvertise smsHomeAdvertise,
     @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        List<SmsHomeAdvertise> list = smsHomeAdvertiseService.findSmsHomeAdvertise(smsHomeAdvertise,pageSize,pageNum);
        if(list.size()>0){
            return CommonResult.success(CommonPage.restPage(list));
        }else {
            return CommonResult.success("未查询到信息");
        }

    }
    /**
     * 添加首页轮播广告表
     */
    @ApiOperation(value = "添加首页轮播广告表")
    @PostMapping(value = "/addSmsHomeAdvertise" )
    public CommonResult addSmsHomeAdvertise(@RequestBody SmsHomeAdvertise smsHomeAdvertise) {

           Integer res= smsHomeAdvertiseService.addSmsHomeAdvertise(smsHomeAdvertise);

        if(res>0){
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败");
    }

    /**
     * 修改首页轮播广告
     */
    @ApiOperation(value = "修改首页轮播广告")
    @PostMapping(value = "/updateSmsHomeAdvertise/{id}" )
    public CommonResult updateSmsHomeAdvertise(@PathVariable("id") Long id,@RequestBody  SmsHomeAdvertise smsHomeAdvertise) {
        int res = 0;
        smsHomeAdvertise.setId(id);
        res = smsHomeAdvertiseService.updateSmsHomeAdvertise(smsHomeAdvertise);

        if (res > 0) {
            return CommonResult.success("修改成功");
        }
        return CommonResult.failed("修改失败");
    }

    /**
     * 修改首页轮播广告
     */
    @ApiOperation(value = "修改首页轮播广告")
    @PostMapping(value = "/updateStatus/{id}" )
    public CommonResult updateStatus(@PathVariable("id") Long id,  SmsHomeAdvertise smsHomeAdvertise) {
        int res = 0;
        smsHomeAdvertise.setId(id);
        res = smsHomeAdvertiseService.updateSmsHomeAdvertise(smsHomeAdvertise);

        if (res > 0) {
            return CommonResult.success("修改成功");
        }
        return CommonResult.failed("修改失败");
    }


    /**
     * 删除首页轮播广告表
     */
    @ApiOperation(value = "删除首页轮播广告表")
    @PostMapping(value = "/delSmsHomeAdvertise" )
    public CommonResult delSmsHomeAdvertise( SmsHomeAdvertiseVo smsHomeAdvertiseVo) {
        Long[] ids= smsHomeAdvertiseVo.getIds();
        int res =0;
        for(int i=0;i<ids.length;i++){
            res = smsHomeAdvertiseService.delSmsHomeAdvertise(ids[i].intValue());
        }
        if(res>0){
            return CommonResult.success("删除成功");
        }
        return CommonResult.failed("删除失败");
    }

   /* *//**
     * 查询首页轮播广告表
     *//*
    @ApiOperation(value = "查询首页轮播广告表")
    @PostMapping(value = "/getSmsHomeAdvertise" )
    public CommonResult getSmsHomeAdvertise( @PathVariable("id") Long id) {
        SmsHomeAdvertise smsHomeAdvertise=smsHomeAdvertiseService.getSmsHomeAdvertise(id);
        return CommonResult.success(smsHomeAdvertise);
    }*/

    @GetMapping ("/getmsHomeAdvertise/{id}")
    @ApiOperation("根据id查询首页轮播广告表")
    public CommonResult getmsHomeAdvertise(@PathVariable("id") Integer id){
        SmsHomeAdvertise smsHomeAdvertise = smsHomeAdvertiseService.getmsHomeAdvertise(id);
        if(smsHomeAdvertise!=null){
            return CommonResult.success(smsHomeAdvertise);
        }
        return CommonResult.failed();
    }
}
